﻿using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Windows.Media;

using Game.ResourceLocator;

namespace Game.Common
{
    /// <summary>
    /// the different players.
    /// </summary>
    [DataContract(Namespace="game")]
    public class GamePlayer : IHasUrl
    {
        readonly string _Name;
        readonly Color _Color;
        readonly string _Url;
        
        internal GamePlayer()
        {
        }

        internal GamePlayer(string name, Color color)
        {
            _Name = name;
            _Color = color;
            _Url = "player:" + _Name;
        }
        
        public string Name
        {
            get { return _Name; }
        }
        
        public virtual bool IsHuman
        {
            get { return false; }
        }
        
        public virtual bool IsAi
        {
            get { return false; }
        }

        public Color Color
        {
            get { return _Color; }
        }
        
        public string Url
        {
            get { return _Url; }
        }
        
        #region Equals and GetHashCode implementation
        
        public override int GetHashCode()
        {
            return _Name.GetHashCode();
        }
        
        public override bool Equals(object obj)
        {
            var other = obj as GamePlayer;
            if (other == null)
                return false;
            return _Name == other._Name;
        }
        
        public static bool operator ==(GamePlayer left, GamePlayer right)
        {
            if (ReferenceEquals(left, right))
                return true;
            if (ReferenceEquals(left, null) || ReferenceEquals(right, null))
                return false;
            return left.Equals(right);
        }
        
        public static bool operator !=(GamePlayer left, GamePlayer right)
        {
            return !(left == right);
        }
        
        #endregion
        
        public override string ToString()
        {
            return Name;
        }

    }
    
    public class PlayerCollection : Collection<GamePlayer>
    {
    }
    
    public class HumanPlayer : GamePlayer
    {
        public static readonly string NAME = "Human";
        public static readonly HumanPlayer Player = new HumanPlayer();
        
        private HumanPlayer()
            : base(NAME, Color.FromArgb(255, 0, 0, 128))
        {
            GameResourceLocator.Register(this);
        }
        
        public override bool IsHuman
        {
            get { return true; }
        }
        
        public static void Register()
        {
            GameResourceLocator.Register(Player);
        }
    }
    
    public class AiPlayer : GamePlayer
    {
        public static readonly string NAME = "AI";
        public static readonly AiPlayer Player = new AiPlayer();
        
        private AiPlayer()
            : base(NAME, Color.FromArgb(255, 128, 0, 0))
        {
            GameResourceLocator.Register(this);
        }
        
        public override bool IsAi
        {
            get { return true; }
        }
        
        public static void Register()
        {
            GameResourceLocator.Register(Player);
        }
    }
}
